/**
* widgets 模块
 * @module widgets
 * @requires base ,core
 */
/**
 左右选择框组件的父类
 @class swordweb.widgets.SwordMove.SwordMoveBase
 * @implements Events
 * @implements Options
 * @extends PageContainer
 **/
/*
/*
*
* 提供一些公用的方法：
*
*
* */
var SwordMoveBase = new Class({

/**
 * 此类的类名称 值使用 SwordMoveBase
 * @property {private final string} name
 */
    name:"SwordMoveBase"
   
   ,readyMove:function(srcWidgets,targetWidgets,type){
        srcWidgets.each(function(src) {
            var operItems=null;
            if(type=='checked'){
                       operItems=src.getCheckedLis();
                  }
            else if(type=='all'){
               operItems=src.getAllLis();
           }
           targetWidgets.each(function(target) {
              if(src.options.moveTid||src.options.moveCtrl){
            	  this.dataContainer = [];
            	  if(this.dataContainer.pushData == undefined) {
            		  this.dataContainer.pushData = function(data, value) {
            			  var postData = [];
            		        if (arguments.length == 2) {
            		            if(value==null||value==undefined)value=''; //解决 如果数字时候后台会报错的问题
            		            var sd = {'name':data,'value':value +''};
            		            postData.push(sd);
            		        } else {
            		            if ($type(data) == "object") {
            		                postData.push(data);
            		            } else if ($type(data) == "array") {
            		                postData = data;
            		            }
            		        }
            		        for (var i = 0; i < postData.length; i++) {
            		            if (!postData[i]["sword"]) {
            		                postData[i]["sword"] = "attr";
            		            }
            		        }
            		        this.combine(postData);
                      }
                  }
            	   var validItems=null;
                   if($defined(src.options.onBeforeOut)&&$chk(src.options.onBeforeOut)){
                       validItems = src.getFunc(src.options.onBeforeOut)[0](operItems,target,this.dataContainer);
                       if(typeof(validItems)=='undefined'){
                          validItems=operItems;
                       }
                      else if(validItems==null||validItems.length==0){
                          return;
                       }
                    }else {
                        validItems=operItems;
                   }
            	  this.dataContainer.pushData(src.getLisData(validItems));
            	  
            	  this.reqData = pc.getReq({
                      'tid':src.options.moveTid
                      ,'ctrl':src.options.moveCtrl
                      ,'widgets':this.dataContainer
                      ,'postType':'ajax'
                  });
	          		
            	  pc.postReq({'req':this.reqData,'async':"true",'encodeData':false,'postType':'ajax'
                      ,'onSuccess':function(res) {
                         var tempItems=validItems;
                         if($defined(target.options.onBeforeIn)&&$chk(target.options.onBeforeIn)){
                              tempItems = target.getFunc(target.options.onBeforeIn)[0](validItems,src);
                              if(typeof(tempItems)=='undefined'){
                              tempItems=validItems;
                            }
                          else if(tempItems==null||tempItems.length==0){
                              return;
                           }
                         }

                         if(tempItems==null||tempItems.length==0){
                              return;
                         }
                    	  src.doMove(target,tempItems);
                          tempItems=null;
                          validItems=null;
                      }.bind(this)
                      , 'onError'  :function(res) {

                      }.bind(this)
                      ,'onFinish':function(res) {
                      }.bind(this)
                  });
              }else{
            	   var validItems=null;
                   if($defined(src.options.onBeforeOut)&&$chk(src.options.onBeforeOut)){
                        validItems = src.getFunc(src.options.onBeforeOut)[0](operItems,target);
                       if(typeof(validItems)=='undefined'){
                          validItems=operItems;
                       }
                      else if(validItems==null||validItems.length==0){
                          return;
                       }
                    }
                     else {
                        validItems=operItems;
                   }
                      var tempItems=validItems;
                    if($defined(target.options.onBeforeIn)&&$chk(target.options.onBeforeIn)){
                        tempItems = target.getFunc(target.options.onBeforeIn)[0](validItems,src);
                          if(typeof(tempItems)=='undefined'){
                          tempItems=validItems;
                       }
                      else if(tempItems==null||tempItems.length==0){
                          return;
                       }
                    }

                    if(tempItems==null||tempItems.length==0){
                          return;
                     }
                  src.doMove(target,tempItems);
                  tempItems=null;
                  validItems=null;
              }
              
            }.bind(this));
        }.bind(this));

    }
    
   ,loadWidget : function(widgetName) {
        var widgetObject = pc.getWidget(widgetName);
        if ($chk(widgetObject)) {
            return widgetObject;
        } else {
            if (this.alarm) {
                alert('无法获取关联组件' + widgetName);
            }
        }
    }
    
});
